package com.hafu.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Copyright 2024 HAFU
 * 类名：DbUtil
 * 包名：com.hafu.util
 * 作者：shencn
 * 描述：实现连接数据库的功能
 * 创建时间：2024/10/30
 * 修改时间：2024/10/30
 */
public class DbUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/db_jxcms1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";
    private static Connection connection = null;

    public static Connection getConn() {
       // 获取Connection对象
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("数据库连接成功");
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    public static void closeConn() {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(DbUtil.getConn());
    }

}
